// 发布订阅模式
class EventEmitter {
    constructor() {
        this.subs = Object.create(null)
    }

    $on(eventType, handler) {
        if (this.subs[eventType] && this.subs[eventType].length) {
            this.subs[eventType].push(handler)
        } else {
            this.subs[eventType] = [handler]
        }
    }

    $emit(eventType, ...args) {
        this.subs[eventType] && this.subs[eventType]?.forEach(handler => {
            handler(...args)
        })
    }

    $off(eventType, handler) {
        if (!this.subs[eventType]) return
        const findHandler = this.subs[eventType]?.findIndex(_handler => _handler = handler)
        findHandler !== null && findHandler !== undefined && this.subs[eventType].splice(findHandler, 1)
    }
}

console.log1(111)
